package www.wen.com.common.lib.helper.loading

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
import com.dylanc.loadingstateview.LoadingStateView
import com.gyf.immersionbar.ImmersionBar
import com.lihang.ShadowLayout
import www.wen.com.common.R
import www.wen.com.common.lib.app.BaseApplication.Companion.CONTEXT
import www.wen.com.common.lib.extension.setOnClickListenerAgency

/**
 * @author WEN
 * @Description:
 * @date 2020/12/8 15:27
 */
class TitleBarUniversalAdapter(
        private var activity: Context,
        private var tilte: String = "",
        private var background: Int = ContextCompat.getColor(CONTEXT, R.color.colorFFFFFF),
        @DrawableRes private var resIdLeft: Int = R.drawable.ic_arrow_left_return_gray,
        @DrawableRes private var resIdRight: Int = R.drawable.blue_ok,
        internal var action: ((view: View) -> Unit)? = null,
        private var isShowLeftText: Boolean = false,
        private var leftText: String = "",
        private var isShowRightImage: Boolean = false,
        private var isShowRightText: Boolean = false,
        private var rightText: String = "",
        private var rightTextSize: Float = 12f,
        private var rightTextColor: Int = ContextCompat.getColor(CONTEXT, R.color.color000000),
        private var isShowSubtitle: Boolean = false,
        private var subtitle: String = "",
        private var subtitleColor: Int = ContextCompat.getColor(CONTEXT, R.color.color888888),
        private var titleTextColor: Int = ContextCompat.getColor(CONTEXT, R.color.color000000),
        private var showSetUnboxBtn: Boolean = false
) : LoadingStateView.ViewDelegate<TitleBarUniversalAdapter.ViewHolder>(),
        View.OnClickListener {

    companion object{
        var IS_FULL_UNPACK = MutableLiveData(false)
    }

    lateinit var mViewHolder: ViewHolder

    override fun onBindViewHolder(holder: ViewHolder) {

        mViewHolder = holder

        holder.titleContainer.setBackgroundColor(background)

        holder.titleView.text = tilte
        holder.ivLeft.setImageResource(resIdLeft)

        holder.titleView.setTextColor(titleTextColor)


        if (isShowSubtitle) {
            holder.tvSubtitle.text = subtitle
            holder.tvSubtitle.setTextColor(subtitleColor)
        }
        holder.tvSubtitle.isVisible = isShowSubtitle

        if (isShowRightText) {
            holder.tvRight.text = rightText
            holder.tvRight.textSize = rightTextSize
            holder.tvRight.setTextColor(rightTextColor)
            holder.tvRight.isVisible = true
        }

        if (isShowRightImage) {
            holder.ivRight.isVisible = true
            holder.ivRight.setImageResource(resIdRight)
        } else {
            holder.ivRight.isVisible = false
        }

        if (isShowLeftText) {
            holder.ivLeft.isVisible = false
            holder.tvLeft.isVisible = true
            holder.tvLeft.text = leftText
        } else {
            holder.ivLeft.isVisible = true
            holder.tvLeft.isVisible = false
        }
        setOnClickListenerAgency(this, holder.ivLeft, holder.ivRight, holder.tvRight, holder.tvLeft)
        ImmersionBar.setTitleBar(activity as Activity, holder.placeholderView)
    }

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        return ViewHolder(inflater.inflate(R.layout.layout_title_universal, parent, false))
    }

    class ViewHolder(val view: View) : LoadingStateView.ViewHolder(view) {

        var titleContainer: ConstraintLayout = view.findViewById(R.id.titleContainer)
        var ivLeft: ImageView = view.findViewById(R.id.ivLeft)
        var tvLeft: TextView = view.findViewById(R.id.tvLeft)
        var placeholderView: View = view.findViewById(R.id.placeholderView)
        var titleView: TextView = view.findViewById(R.id.tvTitle)
        var ivRight: ImageView = view.findViewById(R.id.ivRight)
        var tvRight: TextView = view.findViewById(R.id.tvRight)
        var tvSubtitle: TextView = view.findViewById(R.id.tvSubtitle)

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.ivLeft -> {

            }
        }
        action?.invoke(v)
    }

}